from PyQt6.QtWidgets import QWidget, QVBoxLayout, QHBoxLayout, QLabel, QApplication
from PyQt6.QtCore import Qt
from PyQt6.QtGui import QPixmap
from qfluentwidgets import (LineEdit, PushButton, CardWidget,
                           FluentIcon as FIF, InfoBar, InfoBarPosition,
                           TransparentToolButton, IconWidget)
from dashboard import DashboardWindow
from exercise_app.register import RegisterWindow
from dbutil.db_handler import DatabaseHandler

class IconLineEdit(QWidget):
    def __init__(self, icon, placeholder="", parent=None):
        super().__init__(parent)
        layout = QHBoxLayout(self)
        layout.setContentsMargins(0, 0, 0, 0)
        layout.setSpacing(4)
        
        self.icon = IconWidget(icon, self)
        self.lineEdit = LineEdit(self)
        self.lineEdit.setPlaceholderText(placeholder)
        
        layout.addWidget(self.icon)
        layout.addWidget(self.lineEdit)
        
    def text(self):
        return self.lineEdit.text()
        
    def setEchoMode(self, mode):
        self.lineEdit.setEchoMode(mode)

class LoginWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.setup_ui()
        
    def setup_ui(self):
        self.setWindowTitle("用户登录")
        self.resize(800, 500)
        
        # 主布局
        main_layout = QHBoxLayout()
        
        # 左侧图片区域
        left_widget = CardWidget()
        left_layout = QVBoxLayout()
        
        # 添加欢迎标题
        welcome_label = QLabel("欢迎使用\n智能练习系统")
        welcome_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
        welcome_label.setStyleSheet("""
            QLabel {
                font-size: 28px;
                font-weight: bold;
                color: #1a73e8;
                margin: 20px;
            }
        """)
        
        left_layout.addWidget(welcome_label)
        left_layout.setAlignment(Qt.AlignmentFlag.AlignCenter)
        left_widget.setLayout(left_layout)
        
        # 右侧登录区域
        right_widget = CardWidget()
        right_layout = QVBoxLayout()
        
        # 登录标题
        login_title = QLabel("用户登录")
        login_title.setAlignment(Qt.AlignmentFlag.AlignCenter)
        login_title.setStyleSheet("""
            QLabel {
                font-size: 24px;
                font-weight: bold;
                margin: 20px;
            }
        """)
        
        # 用户名输入
        self.username = IconLineEdit(FIF.PEOPLE, "请输入用户名")
        self.password = IconLineEdit(FIF.PEOPLE, "请输入密码")
        self.password.setEchoMode(LineEdit.EchoMode.Password)
        
        # 记住密码和忘记密码
        option_layout = QHBoxLayout()
        self.remember_btn = TransparentToolButton()
        self.remember_btn.setIcon(FIF.CHECKBOX)
        remember_label = QLabel("记住密码")
        
        self.forget_btn = TransparentToolButton("忘记密码？")
        self.forget_btn.setIcon(FIF.HELP)
        
        option_layout.addWidget(self.remember_btn)
        option_layout.addWidget(remember_label)
        option_layout.addStretch()
        option_layout.addWidget(self.forget_btn)
        
        # 按钮区域
        btn_layout = QHBoxLayout()
        self.login_btn = PushButton("登录")
        self.login_btn.setIcon(FIF.SEND)
        self.register_btn = PushButton("注册账号")
        self.register_btn.setIcon(FIF.ADD)
        
        btn_layout.addWidget(self.login_btn)
        btn_layout.addWidget(self.register_btn)
        
        # 添加控件到右侧布局
        right_layout.addWidget(login_title)
        right_layout.addSpacing(20)
        right_layout.addWidget(self.username)
        right_layout.addSpacing(10)
        right_layout.addWidget(self.password)
        right_layout.addSpacing(10)
        right_layout.addLayout(option_layout)
        right_layout.addSpacing(20)
        right_layout.addLayout(btn_layout)
        right_layout.addStretch()
        
        right_widget.setLayout(right_layout)
        
        # 设置左右两侧比例
        main_layout.addWidget(left_widget, 1)
        main_layout.addWidget(right_widget, 1)
        self.setLayout(main_layout)
        
        # 设置整体样式
        self.setStyleSheet("""
            QWidget {
                background-color: #f5f5f5;
            }
            
            CardWidget {
                border-radius: 8px;
                background-color: white;
                margin: 20px;
            }
            
            LineEdit {
                padding: 8px;
                font-size: 14px;
                border-radius: 4px;
            }
            
            PushButton {
                padding: 8px 24px;
                font-size: 14px;
                font-weight: bold;
                border-radius: 4px;
            }
            
            TransparentToolButton {
                color: #1a73e8;
                font-size: 13px;
            }
        """)
        
        # 绑定事件
        self.login_btn.clicked.connect(self.login)
        self.register_btn.clicked.connect(self.register)
        self.forget_btn.clicked.connect(self.forget_password)
        
    def login(self):
        username = self.username.text()
        password = self.password.text()
        
        # 表单验证
        if not username or not password:
            self.show_info_bar("错误", "请输入用户名和密码", True)
            return
            
        # 数据库验证
        db = DatabaseHandler()
        user = db.verify_login(username, password)
        
        if user:
            self.show_info_bar("成功", "登录成功！", False)
            self.dashboard = DashboardWindow()
            self.dashboard.show()
            self.close()
        else:
            self.show_info_bar("错误", "用户名或密码错误", True)
        
    def register(self):
        self.register_window = RegisterWindow()
        self.register_window.show()
        
    def forget_password(self):
        # TODO: 实现忘记密码功能
        self.show_info_bar("提示", "忘记密码功能开发中...", False)
        
    def show_info_bar(self, title, content, is_error=False):
        """显示信息提示条"""
        InfoBar.success(
            title=title,
            content=content,
            orient=Qt.Orientation.Horizontal,
            isClosable=True,
            position=InfoBarPosition.TOP,
            duration=2000,
            parent=self
        ) if not is_error else InfoBar.error(
            title=title,
            content=content,
            orient=Qt.Orientation.Horizontal,
            isClosable=True,
            position=InfoBarPosition.TOP,
            duration=2000,
            parent=self
        )

if __name__ == "__main__":
    app = QApplication([])
    window = LoginWindow()
    window.show()
    app.exec()